const mongoose = require("../utils/mongoose.js")

// 通过mongoose操作mongodb的 增删改查(CRUD)

// 5.创建模型,

let uSchema = mongoose.Schema({ 
    openid:{
        type: String,
        require: true,
        default: +new Date()
    },
    username: {
        type:String,
        require:true
    },
    password: {
        type:String,
        require:true
    },
    nickname: {
        type:String,
        require:false
    },
    phone: {
        type: String , 
        required: false
    },
    email: { 
        type: String, 
        required: false 
    },
    gender: { 
        type: String, 
        required: false 
    },
        
})

// 6.创建集合

let Collection = mongoose.model("usrs",uSchema);



// 7.往集合中插入一条数据

// 增加 save

const save = (param = {}) => {
    let h5c = new Collection(param)

    return h5c.save() 
        .then(res => res)
        .catch(err => {
            // console.log(err);
            return err.toString(err);
        })
}

// // // 删除 deleteOne/deleteMany

// const dele = (deleo = {}) => {
//    let de = Collection.deleteOne({deleo})
//    return de.dele()
//    .then(res => res)
//    .catch( err => {
//        console.log(err);
//    })
// }


// // 修改 updataOne/updateMany


// const modify = (id,password,email = {}) => {
//     let Modifying = Collection.updateOne({id}, {$set:{password}}, {email})
//     return Modifying.modify() 
//         .then(res => res)
//         .catch(err => {
//             // console.log(err);
//             return err.toString();
//         })
// }

// 查找 find

const find = query => Collection.find(query)
.then(res => res)
.catch(err => {
    console.log(err);
    return err.toString();
})

module.exports = {
    save,
    find,
}